#ifndef classUARTH
 #define classUARTH

 typedef unsigned short Usht;
 typedef unsigned int   Uint;

 #include "CPU_const.h"
 
 

 #define SPort0           0
 #define SPort1           1
 #define SPort2           2
 #define SPort3           3
 #define DataBit5         5
 #define DataBit6         6
 #define DataBit7         7
 #define DataBit8         8
 #define StopBit1         1
 #define StopBit2         2
 #define ParON            true
 #define ParOFF           false
 #define ParODD           0
 #define ParEVEN          1
 #define ParHIGH          2
 #define ParLOW           3
 #define CTSon            true
 #define CTSoff           false
 #define RTSon            true
 #define RTSoff           false
 #define B1200            1
 #define B2400            2
 #define B4800            3
 #define B9600            4
 #define B19200           5
 #define B38400           6
 #define B57600           7
 #define B115200          8
 #define FIFOon          true
 #define FIFOoff         false
 #define FIFOR1           0
 #define FIFOR4           1
 #define FIFOR8           2
 #define FIFOR14          3


/****************************************************************************************************/
/*                              L O W L E V E L     D E F I N E                                     */
/*                               ( d o n ' t     c h a n g e )                                      */
/****************************************************************************************************/
 #define NofUARTs         4          // Number of ARM's UARTS 
 #define NoBaud           0          // No baud
 #define DfltBaud       ( B9600 )    // UART's DeFauLT Baud
 #define NofParTyps       4          // Number of Parity Types
 #define DfltParTyp     ( ParODD )   // Parity DeFauLT Type
 #define DfltFIFORtr    ( FIFOR1 )   // DeFauLT FIFO RxD trigger
 #define FASize           72         // fraction array size
 #define DfltBufSize      264        // default in & out buffer's size
 #define NO_SPort         255
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 









class UART
{
  bool grab, fatal_err;
  char uart, baud, FDReg, LCReg, MCReg, FCReg;
  Usht DLReg;
  char *pbufIN, *pbufOUT;
  Uint Bsize;  

private:  
  bool calc_DLR_FDR(Uint s_baud, Uint pclk);
  
public:
//
  UART(char s_port, char s_baud, Uint pclk);
  UART(char s_port, char s_baud, Uint pclk, char dbit, char sbit, bool par, char parTyp);
//  
  bool setBaud(char s_baud, Uint pclk);
  bool setWLength(char data_bits, char stop_bits);
  bool setParity(bool par, char parTyp);
  bool setFlowCtrl(bool rts_ctrl, bool cts_ctrl);
  bool setFIFO(bool fifo_en, char fifoR_trg);
  Uint setBuffer(Uint size);
//
  bool grabUART(void);
  bool ungrabUART(void);
  bool DTRon(void);
  bool DTRoff(void);
  bool RTS_on(void);
  bool RTS_off(void);
  bool rstFIFOT(void);  
  bool rstFIFOR(void);
//
  bool getFatERR(void)     { return fatal_err; };
  bool getGrab(void)       { return grab; };
  char getUART(void)       { return uart; };
  char getBaud(void)       { return baud; };
  char getDataBits(void)   { return LCReg&0xFC+5; };
  char getStopBits(void)   { return (LCReg>>2)&0xFE+1; };
  bool getParity(void)     { if(LCReg&0x08) {return true;} else {return false;}; };
  char getParType(void)    { return (LCReg>>4)&0xFC; };
  bool getCTSCtrl(void)    { if(MCReg&0x80) {return true;} else {return false;}; };
  bool getRTSCtrl(void)    { if(MCReg&0x40) {return true;} else {return false;}; };
  bool getFIFO(void)       { if(FCReg&0x01) {return true;} else {return false;}; };
  char getFIFO_R_Trg(void) { return (FCReg>>6)&0xFC; };
  Uint getBufSize(void)    { return Bsize; };
//
};


#endif